package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.mapper.UserProfileMapper;
import org.example.pojo.User;
import org.example.pojo.UserProfile;
import org.example.service.UserService;
import org.example.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author Administrator
* @description 针对表【user(用户基础表)】的数据库操作Service实现
* @createDate 2025-04-20 20:01:10
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    private UserMapper  dao;
    @Autowired
    private UserProfileMapper dao2;

    @Override
    public User login(User u) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        if(u.getU().contains("@")){
            u.setEmail(u.getU());
            userQueryWrapper.eq("email",u.getEmail());
        }else if(u.getU().length()==11){
            u.setPhone(u.getU());
            userQueryWrapper.eq("phone",u.getPhone());
        }else {
            System.err.println("登陆失败");
        }


        userQueryWrapper.eq("password",u.getPassword());
        User user = dao.selectOne(userQueryWrapper);


//        对最后登录时间进行修改
        User user1 = new User();
        user1.setLastLoginTime(new Date());
        user1.setId(user.getId());
        dao.updateById(user1);
        System.err.println(user1.getLastLoginTime());
        System.err.println("登录成功");
        return user;
    }

    @Override
    public int add(User u) {
        u.setLastLoginTime(new Date());
        u.setAvatarUrl("http://aliyun_id_photo_bucket.oss.aliyuncs.com/default_handsome.jpg");
        if(u.getUsername().length()<=7){
            dao.insert(u);
            UserProfile userProfile = new UserProfile();
            userProfile.setUid(u.getId());
            dao2.insert(userProfile);
            System.err.println(u+"添加成功");
            return 1;
        }else {
            System.err.println("添加失败");
            return 0;
        }
    }

    @Override
    public User upd(User u,String passwordx) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        if (u.getU().contains("@")) {
            u.setEmail(u.getU());
            userQueryWrapper.eq("email", u.getEmail());
            User user = dao.selectOne(userQueryWrapper);
            user.setPassword(passwordx);
            dao.updateById(user);

        } else if (u.getU().length() == 11) {
            u.setPhone(u.getU());
            userQueryWrapper.eq("phone", u.getPhone());
            User user = dao.selectOne(userQueryWrapper);
            user.setPassword(passwordx);
            dao.updateById(user);
        }else {
            System.err.println("没有该账号的信息");
        }
        return u;
    }


    @Override
    public int update(int id, String password) {
        User user = dao.selectById(id);
        user.setPassword(password);
        int i = dao.updateById(user);
        System.err.println("修改成功");
        return i;
    }


}




